//
//  PanPushAnimationFromRight.m
//  IfengNews
//
//  Created by Ryan on 13-10-29.
//
//

#import "PanPushAnimationFromRight.h"
#import "CLBaseViewController.h"
#import "CLPushAnimatedRight.h"


@implementation PanPushAnimationFromRight


- (void)doAnimation:(UIPanGestureRecognizer *)recognizer{
    
    CGPoint point=[recognizer locationInView:[self k_mainWindow]];
    
    if(recognizer.state == UIGestureRecognizerStateBegan){
        self.startPoint=point;
        CGPoint v=[recognizer velocityInView:[self k_mainWindow]];
        
        if(self.moveStatus==Dispatch_Animation_status_ready &&
           v.x<0 &&fabsf(v.y/v.x)<0.5){
            [self begin];
       
            
        }

        
    }else if(recognizer.state == UIGestureRecognizerStateEnded){
        if(self.moveStatus==Dispatch_Animation_status_Move){
            if (point.x-self.startPoint.x  < -100){
                [self push];
            }else{
                [self cancle];
            }
            self.moveStatus=Dispatch_Animation_status_decelerate;
        }
        
    } else if(recognizer.state==UIGestureRecognizerStateCancelled){
        if(self.moveStatus==Dispatch_Animation_status_Move){
            [self cancle];
        }
   
       
    }
    
   
    
    if(self.moveStatus==Dispatch_Animation_status_Move){
        
        [self moveViewWithX:point.x-self.startPoint.x];
    }
    
}

- (void)forceBegin{
    [self begin];

}

- (void)pushDirectly{
    [self forceBegin];
    [self push];
}


- (void)begin{
    if(![[CLGloble sharedInstance] checkAnimationLock:self])return;
    if(self.moveStatus!=Dispatch_Animation_status_ready)return;
    if(!self.baseVC){
        return;
    }else{
        
        self.baseVC.preSnapShot=[[self k_currentController].view snapshotViewAfterScreenUpdates:NO];
    }
    self.moveStatus=Dispatch_Animation_status_Move;
   
    CGRect rect=[self k_rootController].view.bounds;
    
    self.gusetView=[[UIView alloc] init];
    self.gusetView.userInteractionEnabled=YES;
    self.gusetView.frame=rect;
   
    self.imgView=self.baseVC.preSnapShot;
    self.imgView.frame=self.gusetView.bounds;
    
    self.markView=[[UIView alloc] initWithFrame:self.gusetView.bounds];
    self.markView.userInteractionEnabled=NO;
    self.markView.backgroundColor=[UIColor blackColor];
    self.markView.alpha=0.0f;
    
    self.baseVC.view.frame=CGRectMake(GlobleWidth, 0, GlobleWidth, rect.size.height);
    [self.gusetView addSubview:self.imgView];
    [self.imgView addSubview:self.markView];
    [self.gusetView addSubview:self.baseVC.view];
     //[self.entity.contoller.view addSubview:[DispatchMap sharedInstance].leftShadom];
//    NSLog(@"%@",[DispatchMap sharedInstance].leftShadom);
  // [DispatchMap sharedInstance].leftShadom.backgroundColor=[UIColor orangeColor];
    
//    if(self.touchHandle){
//        self.realTouchHandleSuperView=self.touchHandle.superview;
//        CGRect frame=self.touchHandle.frame;
//        frame.origin.x+=320.0f;
//        self.touchHandle.frame=frame;
//        [self.entity.contoller.view addSubview:self.touchHandle];
//    }
    [[self k_rootController].view addSubview:self.gusetView];
    
    self.isBegin=YES;
    
}


- (void)cancle{
    if(!self.isBegin) return;
    [UIView animateWithDuration:0.2 animations:^{
        [self moveViewWithX:0];
    } completion:^(BOOL finish){
      
        CGRect frame=self.touchHandle.frame;
        frame.origin.x=0;
        self.touchHandle.frame=frame;
        [self.realTouchHandleSuperView addSubview:self.touchHandle];
        [self.baseVC.view removeFromSuperview];
        [self.gusetView removeFromSuperview];
        self.baseVC.preSnapShot=nil;
        self.gusetView=nil;
        self.imgView=nil;
        self.markView=nil;
        [[CLGloble sharedInstance] cleanAnimationLock];
         self.moveStatus=Dispatch_Animation_status_ready;
    }];
    self.isBegin=NO;
}

- (void)push{
    if(!self.isBegin) return;
    [UIView animateWithDuration:0.2 animations:^{
        [self moveViewWithX:-GlobleWidth];
    } completion:^(BOOL finish){
        
        [self.baseVC.view removeFromSuperview];
        [self.gusetView removeFromSuperview];
        [[CLPushAnimatedRight sharedInstance] doNavigationPuhs:self.baseVC];
        self.gusetView=nil;
        self.imgView=nil;
        self.markView=nil;
        [[CLGloble sharedInstance] cleanAnimationLock];
         self.moveStatus=Dispatch_Animation_status_ready;
        
    }];
    
    self.isBegin=NO;
    
}

- (void)moveViewWithX:(float )x{
    x = x<-320?-320:x;
    x = x>0?0:x;
    UIView *naviView=self.baseVC.view;
    CGRect frame = naviView.frame;
    float w=frame.size.width;
    float m=w+x;
    frame.origin.x = m>0?m:0;
    naviView.frame = frame;
    frame=self.imgView.frame;
    
    self.markView.alpha=DispatchPopMaskAlpha*(-(x/w));
    frame.origin.x=-(Dispatch_distance_x2+Dispatch_distance_x1*((x+w)/320.0f))*w+(x+w);
    //frame.origin.x=x*Dispatch_distance_x2;
    self.imgView.frame=frame;
    
    
}

@end
